package pbx.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pbx.model.Queue;
import pbx.model.QueueLogSummary;
import util.DateUtil;


@Stateless
public class QueueLogSummaryService {

	@EJB
	QueueService queueService;
	
	@EJB
	DateUtil dateUtil;
	
	@PersistenceContext(unitName = "persistence")
    private EntityManager em;
	
	
	public QueueLogSummary save(QueueLogSummary queuelogSummaryHour) {
		em.persist(queuelogSummaryHour);
		return queuelogSummaryHour;   
	}
	
	public QueueLogSummary update(QueueLogSummary queuelogSummaryHour) {
		return em.merge(queuelogSummaryHour);
	}
	
	public QueueLogSummary getByHour(Integer year, Integer month, Integer day, Integer hour, Queue queue){
    	
		System.out.println("YEAR "+ year);
		System.out.println("MONTH "+ month);
		System.out.println("DAY "+ day);
		System.out.println("HOUR "+ hour);
		System.out.println("QUEUE "+ queue);
				
		Date calldate = dateUtil.customDate(year, month, day, hour, 0, 0, 0);
		
		System.out.println("CUSTOM DATE "+ calldate);
		
		QueueLogSummary result = null;
		String sql = " SELECT e FROM QueueLogSummary e WHERE e.calldate=:calldate AND e.type=:type AND e.queue=:queue ";
		Query query = em.createQuery(sql);
		query.setParameter("calldate", calldate);
    	query.setParameter("queue", queue);
    	query.setParameter("type", "hour");
    	
    	
    	System.out.println("QUERY " + query.toString());
    	try {
    		result = (QueueLogSummary) query.getSingleResult();
        }
    	catch (NoResultException e) {
			//e.printStackTrace();
		}
        return result;
    }
	
	@SuppressWarnings("unchecked")
	public List<QueueLogSummary> listByHour(Integer year, Integer month, Integer day, Integer hour, List<Queue> queues){
    	
		Date calldate = dateUtil.customDate(year, month, day, hour, 0, 0, 0);
		
		List<QueueLogSummary> result = null;
		String sql = " SELECT e FROM QueueLogSummary e WHERE e.calldate=:calldate AND e.type=:type ";
		for (Queue queue : queues ) {
    		sql  =  sql + " OR queue="+queue;
    	}
    	Query query = em.createQuery(sql);
    	query.setParameter("calldate", calldate);
    	query.setParameter("type", "hour");
    	try {
    		result = query.getResultList();
        }
    	catch (NoResultException e) {
			//e.printStackTrace();
		}        	
		return result; 
    }

	public QueueLogSummary getSumByHour(Integer year, Integer month, Integer day, Integer hour, List<Queue> queues){
		
		QueueLogSummary result = new QueueLogSummary();
		result.setEnterqueue(0);
		result.setConnect(0);
		result.setAbandon(0);
		
		Iterator<Queue> queuesIt = queues.iterator();
		while(queuesIt.hasNext()){
			Queue queue = queuesIt.next();
			QueueLogSummary queueLogSummaryHour = getByHour(year, month, day, hour, queue);
			if(queueLogSummaryHour != null){
				result.setEnterqueue(result.getEnterqueue() + queueLogSummaryHour.getEnterqueue());
				result.setConnect(result.getConnect() + queueLogSummaryHour.getConnect());
				result.setAbandon(result.getAbandon() + queueLogSummaryHour.getAbandon());	
			}
		}
		return result;
	}
	
    
}

